package com.itboye.jinpuyiyuan.activity.home.testserver;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.activity.pay.PayActivity;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.AppointPhysicalBean;
import com.itboye.jinpuyiyuan.bean.PhysicalListBean;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.YuYueChooseTimePopwindow;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2017/12/13 0013.
 */

public class TestServerFixedQueryDetailActivity extends BaseActivity implements YuYueChooseTimePopwindow.onFinishedLisenter, Observer {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;
    private TextView confirmPayTv;
    private  PhysicalListBean.PhysicalBean bean;

    private TextView name;
    private TextView newPrice;
    private TextView oldPrice;
    private TextView departments;
    private TextView contents;
    private ImageView topImg;

    private String department_names="";
    private String department_contents="";

    private String uid;
    private BranchAddressPresenter presenter;
    private String fee;
    private String menuId;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_testserver_fixeddetail);
//        intent.putExtra("item",physicalBean);
        bean= (PhysicalListBean.PhysicalBean) getIntent().getSerializableExtra("item");
        rightImg.setVisibility(View.GONE);
        leftTv.setVisibility(View.GONE);
        tvTitle.setText("产品详情");

        uid= SPUtils.get(this,null, Const.UID,"")+"";
        presenter=new BranchAddressPresenter(this);
        menuId=bean.getPACKAGEID();

        name.setText(bean.getName());
        if(bean.getSex().trim().equals("0")){ //通用
            topImg.setImageResource(R.drawable.big_nn);
        }else if (bean.getSex().trim().equals("1")){//男性
            topImg.setImageResource(R.drawable.big_man);
        }else if(bean.getSex().trim().equals("2")){//女性
            topImg.setImageResource(R.drawable.big_woman);
        }
        SimpleDateFormat format=new SimpleDateFormat("yyyy-M-dd hh:mm:ss");
        if (!"".equals(bean.getDiscountBeginDate())){
            Date start,end;
            try {
                start= format.parse(bean.getDiscountBeginDate());
                end= format.parse(bean.getDiscountEndDate());
                if (System.currentTimeMillis()>=start.getTime()&&System.currentTimeMillis()<=end.getTime()){
                    newPrice.setText("¥"+bean.getDiscountPrice());
                    oldPrice.setText("原价¥"+bean.getPrice());
                    oldPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG); //中划线
                }else {
                    newPrice.setText("¥"+bean.getPrice());
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else {
           newPrice.setText("¥"+bean.getPrice());
        }

        for (int i=0;i<bean.getItem_list().size();i++){
            if (i==bean.getItem_list().size()-1){
//                department_names+=bean.getItem_list().get(i).getDeptname();
                department_contents+=bean.getItem_list().get(i).getDeptname()+"("+
                        bean.getItem_list().get(i).getItemname()+")";
            }else {
//                department_names+=bean.getItem_list().get(i).getDeptname();
                department_contents+=bean.getItem_list().get(i).getDeptname()+"("+
                        bean.getItem_list().get(i).getItemname()+"),";
            }
        }
        contents.setText(department_contents);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;
            case R.id.confirmPayTv:
                showTimePop(view);
//                Toast.makeText(this,"待开发",Toast.LENGTH_SHORT).show();
                break;
        }
    }


    public void showTimePop(View v){
        YuYueChooseTimePopwindow chooseTimePopwindow = new YuYueChooseTimePopwindow(this,this,true);
        chooseTimePopwindow.getContentView().findViewById(R.id.time).setVisibility(View.GONE);
        chooseTimePopwindow.getContentView().findViewById(R.id.min).setVisibility(View.GONE);
        chooseTimePopwindow.showAtLocation(v, Gravity.BOTTOM
                | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    public void getBeginTime(String year, String month, String day, String hour, String minite) {
//        MAlert.alert("start"+year+"-"+month+"-"+day+" "+hour+":"+minite);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");

        String start = bean.getDiscountBeginDate();
        String endDate = bean.getDiscountEndDate();

        try {
            Date parse = format.parse(year + "-" + month + "-" + day);
            if (!"".equals(start)){
                Date parse1 = format.parse(start);
                Date parse2 = format.parse(endDate);
                Log.d("date===","now="+parse.getTime()+"start="+parse1.getTime()+"end="+parse2.getTime());
                if (parse.getTime()>=parse1.getTime()&&parse.getTime()<=parse2.getTime()){
                    fee=Float.parseFloat(bean.getDiscountPrice())*100+"";
                }else {
                    fee=Float.parseFloat(bean.getPrice())*100+"";
                }
            }else {
                fee=Float.parseFloat(bean.getPrice())*100+"";
            }
            presenter.physicalOrder(uid,"1",menuId,"",fee,parse.getTime()/1000+"");
        } catch (ParseException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void getEndTime(String year, String month, String day, String hour, String minite) {

        MAlert.alert("end"+year+"-"+month+"-"+day+" "+hour+":"+minite);
    }

    @Override
    public void update(Observable o, Object arg) {

        ResultEntity entity = handlerError(arg);
        if (entity!=null){
            if (entity.getEventType()== BranchAddressPresenter.By_Hospitalization_appointment_success){ //预约体检确认

                //跳转支付界面
//                orderId = getIntent().getStringExtra("order_id");
//                remark = getIntent().getStringExtra("remark");
                AppointPhysicalBean bea= (AppointPhysicalBean) entity.getData();
                Intent intent = new Intent(this, PayActivity.class);
                intent.putExtra("order_id",bea.getOrder_code());
                intent.putExtra("remark",bea.getNote());
                startActivity(intent);
                finish();
            }else if (entity.getEventType() ==BranchAddressPresenter.By_Hospitalization_appointment_fail){

                MAlert.alert(entity.getMsg());
            }
        }
    }
}
